//
//  PersonalHomePageView.m
//  SHHCPhone
//
//  Created by SL on 14-7-15.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "PersonalHomePageView.h"
#import "UIButton+WebCache.h"
#import "NationClass.h"

@implementation PersonalHomePageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [doneButton setTitle:@"确认" forState:UIControlStateNormal];
        [self againShow:([APP_USER_MSG.pregnancy integerValue]-1)];
        isFirst = YES;
    }
    return self;
}

-(void)showTZInput:(BOOL)show
{
    if (isFirst) {
        [self againShow:seletView2.currentType];
    }
}

-(void)againShow:(NSInteger)index
{
    index = index<0?1:index;
    CGFloat y = 0;
    if (index != 0) {
        [field5 setHidden:NO];
        [field6 setHidden:NO];
        y = field6.frame.size.height+field6.frame.origin.y;
    }else{
        [field5 setHidden:YES];
        [field6 setHidden:YES];
        y = seletView2.frame.size.height+seletView2.frame.origin.y;
    }
    [self selectZ:y index:index];
    
    UIView *view = (UIView*)[viewArray lastObject];
    [doneButton setCenter:CGPointMake(seletView2.center.x, view.frame.size.height+view.frame.origin.y+31+doneButton.frame.size.height/2)];
    [mainScroll setContentSize:CGSizeMake(0, doneButton.frame.size.height+doneButton.frame.origin.y+10)];
    
    [self showContent];
}

-(void)showContent
{
    if (!isFirst) {
        if (APP_USER_MSG.headimg.length > 0) {
            [headButton setImageWithURL:[NSURL URLWithString:APP_USER_MSG.headimg] placeholderImage:[UIImage imageNamed:@"完善资料页-上传头像按钮.png"]];
            [headButton setImage:nil forState:UIControlStateHighlighted];
            [headButton.layer setCornerRadius:MIN(headButton.frame.size.height, headButton.frame.size.width)/2];
            [headButton.layer setMasksToBounds:YES];
        }
        
        if (APP_USER_MSG.nickname.length > 0 ) {
            field1.textField.text = APP_USER_MSG.nickname;
        }
        
        if (APP_USER_MSG.birthday.length > 0 ) {
            NSDate *date = [z_DateTime dencodeTime:APP_USER_MSG.birthday format:@"yyyy-MM-dd"];
            field2.textField.text = [z_DateTime encodeTime:date format:APP_SELECTPICKERVIEW_DATE_FORMAT];
        }
        
        if (APP_USER_MSG.height.length > 0 && [APP_USER_MSG.height integerValue]!=0) {
            field3.textField.text = [NSString stringWithFormat:@"%@%@",APP_USER_MSG.height,APP_LENGTH_UNIT];
        }
        
        if (APP_USER_MSG.nation.length > 0 ) {
            field4.textField.text = [NationClass getIDName:APP_USER_MSG.nation];
        }
        
        //避免设置当前位置的时候执行代理方法
        seletView1.delegate = nil;
        seletView2.delegate = nil;
        
        if ([APP_USER_MSG.amount integerValue] != 0 ) {
            seletView1.currentType = [APP_USER_MSG.amount integerValue]-1;
        }
        
        if ([APP_USER_MSG.pregnancy integerValue] != 0 ) {
            seletView2.currentType = [APP_USER_MSG.pregnancy integerValue]-1;
        }
        
        seletView1.delegate = self;
        seletView2.delegate = self;
        
        if (APP_USER_MSG.last_menstrual.length != 0 ) {
            NSDate *date = [z_DateTime dencodeTime:APP_USER_MSG.last_menstrual format:@"yyyy-MM-dd"];
            field5.textField.text = [z_DateTime encodeTime:date format:APP_SELECTPICKERVIEW_DATE_FORMAT];
        }
        
        if (APP_USER_MSG.weight.length > 0 && [APP_USER_MSG.weight integerValue]!=0) {
            field6.textField.text = [NSString stringWithFormat:@"%.1f%@",[APP_USER_MSG.weight floatValue],APP_WEIGHT_UNIT];
        }
    }
    CLog(@"APP_UPDATE_USER_MSG===%@",APP_UPDATE_USER_MSG);
    NSArray *array = @[@[APP_USER_MSG.older_monther?APP_USER_MSG.older_monther:@"0",APP_USER_MSG.family_disease?APP_USER_MSG.family_disease:@"0",APP_USER_MSG.habitual_abortion?APP_USER_MSG.habitual_abortion:@"0"],@[APP_USER_MSG.high_blood_pressure?APP_USER_MSG.high_blood_pressure:@"0",APP_USER_MSG.high_blood_sugar?APP_USER_MSG.high_blood_sugar:@"0",APP_USER_MSG.anaemia?APP_USER_MSG.anaemia:@"0"],@[APP_USER_MSG.high_blood_fat?APP_USER_MSG.high_blood_fat:@"0",APP_USER_MSG.high_blood_pressure?APP_USER_MSG.high_blood_pressure:@"0",APP_USER_MSG.high_blood_sugar?APP_USER_MSG.high_blood_sugar:@"0"]];
    array = [array objectAtIndex:seletView2.currentType];
    for (int i = 0 ; i < viewArray.count; i++) {
        PerfectContentTwoView *view = (PerfectContentTwoView*)[viewArray objectAtIndex:i];
        if ([[array objectAtIndex:i]integerValue]) {
            [view setSelected:YES];
        }else{
            [view setSelected:NO];
        }
    }
}

-(void)selectZ:(CGFloat)y index:(NSInteger)index
{
    for (UIView *view in viewArray) {
        [view removeFromSuperview];
    }
    
    NSArray *dataArray = nil;
    switch (index) {
        case 0:
        {
            //孕前
            dataArray = @[@"高龄产妇",@"家族疾病史",@"习惯性流产"];
        }
            break;
        case 1:
        {
            //孕中
            dataArray = @[@"血压高",@"血糖高",@"贫血"];
        }
            break;
        case 2:
        {
            //孕后
            dataArray = @[@"肥胖",@"血压高",@"血糖高"];
        }
            break;
        default:
            break;
    }
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    for (int i = 0 ; i < dataArray.count; i++) {
        PerfectContentTwoView *view = [[PerfectContentTwoView alloc] initWithFrame:CGRectMake((CURRENT_CONTENT_WIDTH-220)/2, 27+50*i+y, 220, 30)];
        [mainScroll addSubview:view];
        [view setContent:[dataArray objectAtIndex:i]];
        [view setSelected:NO];
        [array addObject:view];
    }
    viewArray = array;
}

//添加信息
-(NSDictionary*)addMsg:(NSDictionary*)dic
{
    [self saveImage:headButton.imageView.image];
    
    NSArray *array = @[@[@"older_monther",@"family_disease",@"habitual_abortion"],@[@"high_blood_pressure",@"high_blood_sugar",@"anaemia"],@[@"high_blood_fat",@"high_blood_pressure",@"high_blood_sugar"]];
    array = [array objectAtIndex:seletView2.currentType];
    NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:dic];
    for (int i = 0 ; i < viewArray.count; i++) {
        [d setObject:[NSNumber numberWithInt:((PerfectContentTwoView*)[viewArray objectAtIndex:i]).selected] forKey:[array objectAtIndex:i]];
    }
    return d;
}

@end
